<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { get, isWechat } from '@/utils/service'
import { VUE_APP_BASE_API } from '@/utils/baseConfig'
import { initWehcatShare, shareType } from '@/utils/share'
import sharePerson from '@/components/sharePerson.vue'
const router = useRoute()
const dbid = router.query.dbid
const shareMemberId = router.query.shareMemberId || -1
const shareRecordNo = router.query.shareRecordNo || ''
const shareMemberState = ref(false)
const corporateCulture = ref({})
const shareMemberInfo = ref({})
const LIKE_NORMAL_IMAGE = VUE_APP_BASE_API + '/images/vue/like.png'
const LIKE_LIKED_IMAGE = VUE_APP_BASE_API + '/images/vue/liked.png'
const likeImage = ref(LIKE_NORMAL_IMAGE)
const isWechatState = ref(false)
const richTextContent = ref()
const shareData = ref({})
const url = window.location.href.split('#')[0]
onMounted(() => {
  try {
    isWechatState.value = isWechat()
    get('/corporateCultureWechatNew/readAgentRewardWechat', {
      corporateCultureId: dbid,
      shareCode: shareRecordNo,
    }).then((res) => {
      const data = res.data
      shareData.value.title = data.title
      shareData.value.desc = data.description
      shareData.value.link = url
      shareData.value.imgUrl = VUE_APP_BASE_API + data.picture
      corporateCulture.value = data
      if (corporateCulture.value.likeState == 1) {
        likeImage.value = LIKE_NORMAL_IMAGE
      } else {
        likeImage.value = LIKE_LIKED_IMAGE
      }
      richTextContent.value = addDomainToSrcWithDOM(
        corporateCulture.value.content,
        VUE_APP_BASE_API,
      )
      initWehcatShare(shareData.value, url, isWechatState.value, shareType.SHARE_WECHAT_NEWS) //生成分享数据
    })
    getShareMember()
  } catch (error) {
    console.log('onMounted error:' + error)
  }
})
function getShareMember() {
  //通过分享会员信息获取个人名片信息
  get('/memberWechatNew/queryWechatShareMemberById', {
    shareMemberId: shareMemberId,
  }).then((res) => {
    if (res.code == 0) {
      shareMemberState.value = true
      const data = res.data
      shareMemberInfo.value = data
      if (data.user == null) {
        shareMemberState.value = false
      }
    } else {
      shareMemberState.value = false
    }
  })
}
function likeNews() {
  const likeState = corporateCulture.value.likeState
  get('/corporateCultureWechatNew/saveParseCorporateCultureRecordWechat', {
    corporateCultureId: dbid,
    type: likeState,
  }).then((res) => {
    const data = res.data
    if (data != null) {
      corporateCulture.value.likeState = data.likeState
      if (data.likeState == 1) {
        likeImage.value = LIKE_NORMAL_IMAGE
      } else {
        likeImage.value = LIKE_LIKED_IMAGE
      }
      corporateCulture.value.praiseNum = data.praiseNum || 0
    }
  })
}
function addDomainToSrcWithDOM(html: string, domain: string) {
  const parser = new DOMParser()
  const doc = parser.parseFromString(html, 'text/html')
  const images = doc.querySelectorAll('img')

  images.forEach((img) => {
    const src = img.getAttribute('src') || null
    if (null != src && !src.startsWith('http')) {
      img.setAttribute('src', `${domain}${src}`)
    }
  })

  return new XMLSerializer().serializeToString(doc.body)
}
//
</script>
<template>
  <div style="padding: 12px">
    <div>
      <h2>{{ corporateCulture.title }}</h2>
    </div>
    <div>
      <h4>
        发布人:{{ corporateCulture.realessPerson }} &nbsp;&nbsp;&nbsp;&nbsp; 发布时间:{{
          corporateCulture.realessTime
        }}
      </h4>
    </div>
    <sharePerson :item="shareMemberInfo" v-if="shareMemberState" />
    <div class="newsRichContent" v-html="richTextContent"></div>
    <div>
      恭祝以上人员获得成都瑞一汽车月度经纪人红包奖励，感谢各位对成都瑞一汽车的鼎力支持和信任，经纪人喜报下月再见。
      如果大家还不知道瑞一经纪人、经纪人政策，请点<RouterLink
        to="/rewardRole"
        style="color: var(--main-color)"
        >经纪人政策</RouterLink
      >查看。
    </div>
    <div class="buyChery">买奇瑞一定来瑞一看看！！！</div>
    <van-row class="" style="line-height: 30px">
      <van-col span="6"
        >阅读量：<span class="newNum">{{ corporateCulture.pvNum }}</span></van-col
      >
      <van-col span="6"
        >点赞量：<span class="newNum">{{ corporateCulture.praiseNum }}</span></van-col
      >
      <van-col span="6"
        >分享量：<span class="newNum">{{ corporateCulture.shareNum }}</span></van-col
      >
      <van-col
        span="6"
        style="text-align: right; cursor: pointer"
        v-if="isWechatState"
        :onclick="likeNews"
      >
        <img :src="likeImage" alt="" width="30" height="30" />
      </van-col>
    </van-row>
    <br />
    <br />
    <br />
    <div class="newsFooter">
      <RouterLink to="/" class="router-link-active" v-if="isWechatState">返回主页</RouterLink>
      <div class="copyright">Copyright@2025 成都易成致达网络科技提供技术支持</div>
    </div>
  </div>
  <br />
</template>
